package com.wkt.ggkt.wechat.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wkt.ggkt.model.wechat.Menu;
import com.wkt.ggkt.vo.wechat.MenuVo;
import com.wkt.ggkt.wechat.mapper.MenuMapper;
import com.wkt.ggkt.wechat.service.MenuService;
import lombok.SneakyThrows;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 订单明细 订单明细 服务实现类
 * </p>
 *
 * @author wkt
 * @since 2023-12-27
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

    @Resource
    private MenuMapper menuMapper;
    @Resource
    private WxMpService wxMpService;

    @Override
    public List<MenuVo> findMenuInfo() {
        List<MenuVo> finalList = new ArrayList<>();
        // 1、先找到所有的一级菜单
        List<Menu> oneLevelMenuList = this.findMenuOneInfo();
        List<MenuVo> menuVoList = JSONObject.parseArray(JSONObject.toJSONString(oneLevelMenuList), MenuVo.class);
        finalList.addAll(menuVoList);
        if (!CollectionUtils.isEmpty(finalList)) {
            recursionGetChild(finalList);
        }

        return finalList;
    }

    private void recursionGetChild(List<MenuVo> finalList) {
        for (MenuVo menu : finalList) {
            // 2、找子集
            List<MenuVo> childList = getChildrenMenuList(menu.getId());
            menu.setChildren(childList);
            if (!CollectionUtils.isEmpty(childList)) {
                recursionGetChild(childList);
            }
        }
    }

    private List<MenuVo> getChildrenMenuList(Long id) {
        QueryWrapper<Menu> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",id);
        List<Menu> menus = menuMapper.selectList(wrapper);
        return JSONObject.parseArray(JSONObject.toJSONString(menus),MenuVo.class);
    }

    @Override
    public List<Menu> findMenuOneInfo() {
        QueryWrapper<Menu> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id","0");
        List<Menu> menus = menuMapper.selectList(wrapper);
        return menus;
    }

    @SneakyThrows
    @Override
    public void syncMenu() throws WxErrorException {
        List<MenuVo> menuVoList = this.findMenuInfo();
        //菜单
        JSONArray buttonList = new JSONArray();
        for(MenuVo oneMenuVo : menuVoList) {
            JSONObject one = new JSONObject();
            one.put("name", oneMenuVo.getName());
            JSONArray subButton = new JSONArray();
            for(MenuVo twoMenuVo : oneMenuVo.getChildren()) {
                JSONObject view = new JSONObject();
                view.put("type", twoMenuVo.getType());
                if(twoMenuVo.getType().equals("view")) {
                    view.put("name", twoMenuVo.getName());
                    view.put("url", "http://ggkt2.vipgz1.91tunnel.com/#"
                            +twoMenuVo.getUrl());
                } else {
                    view.put("name", twoMenuVo.getName());
                    view.put("key", twoMenuVo.getMeunKey());
                }
                subButton.add(view);
            }
            one.put("sub_button", subButton);
            buttonList.add(one);
        }
        //菜单
        JSONObject button = new JSONObject();
        button.put("button", buttonList);
        this.wxMpService.getMenuService().menuCreate(button.toJSONString());
    }

    @Override
    public void removeMenu() throws WxErrorException {
        wxMpService.getMenuService().menuDelete();
    }


}
